package controllers

import (
	"encoding/json"
	"ltbz/cultural_tourism_api/models"
	"ltbz/cultural_tourism_api/utils"
	"strings"
	"time"
)

// 景区
type ScenicSpotController struct {
	BaseAuthController
}

// @Title 添加景区
// @Description 添加景区
// @Param	request	body models.ScenicSpotAddReq true "type json string"
// @Success 200
// @router /add [post]
func (this *ScenicSpotController) Add() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req models.ScenicSpotAddReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.ScenicSpotName == "" {
		br.Msg = "景区名称不能为空"
		return
	}

	ssObj := new(models.ScenicSpot)
	ssObj.ScenicSpotName = req.ScenicSpotName
	ssObj.Address = req.Address
	ssObj.Status = "启用"
	ssObj.CreateTime = time.Now()
	ssObj.ModifyTime = time.Now()
	_, err = ssObj.Add()
	if err != nil {
		if strings.Contains(err.Error(), "scenic_spot.idx_scenic_spot_name") {
			br.Msg = "景区名称已经存在，请重新填写"
			return
		}
		br.ErrMsg = "保存失败，Err:" + err.Error()
		br.Msg = "保存失败"
		return
	}
	br.Msg = "保存成功"
	br.Ret = 200
	return
}

// @Title 编辑景区
// @Description 编辑景区
// @Param	request	body models.ScenicSpotEditReq true "type json string"
// @Success 200
// @router /edit [post]
func (this *ScenicSpotController) Edit() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req models.ScenicSpotEditReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}

	if req.ScenicSpotId <= 0 {
		br.Msg = "参数错误"
		return
	}

	if req.ScenicSpotName == "" {
		br.Msg = "景区名称不能为空"
		return
	}

	ssObj := new(models.ScenicSpot)
	updateParams := make(map[string]interface{})
	updateParams["scenic_spot_name"] = req.ScenicSpotName
	updateParams["address"] = req.Address
	updateParams["modify_time"] = time.Now()

	whereParam := make(map[string]interface{})
	whereParam["scenic_spot_id"] = req.ScenicSpotId

	err = ssObj.Update(updateParams, whereParam)
	if err != nil {
		br.ErrMsg = "保存失败，Err:" + err.Error()
		br.Msg = "保存失败"
		return
	}
	br.Msg = "保存成功"
	br.Ret = 200
	return
}

// @Title 启用/禁用景区
// @Description 启用/禁用景区
// @Param	request	body models.ScenicSpotEnableOrDisableReq true "type json string"
// @Success 200
// @router /enable_or_disable [post]
func (this *ScenicSpotController) EnableOrDisable() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req models.ScenicSpotEnableOrDisableReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.ScenicSpotId <= 0 {
		br.Msg = "参数错误"
		return
	}

	ssObj := new(models.ScenicSpot)
	item, err := ssObj.GetScenicSpotById(req.ScenicSpotId)
	if err != nil {
		br.Msg = "保存失败"
		br.ErrMsg = "保存失败,Err:" + err.Error()
		return
	}

	updateParams := make(map[string]interface{})
	if item.Status == "禁用" {
		updateParams["status"] = "启用"
	} else {
		updateParams["status"] = "禁用"
	}

	updateParams["modify_time"] = time.Now()

	whereParam := make(map[string]interface{})
	whereParam["scenic_spot_id"] = req.ScenicSpotId

	err = ssObj.Update(updateParams, whereParam)
	if err != nil {
		br.ErrMsg = "保存失败，Err:" + err.Error()
		br.Msg = "保存失败"
		return
	}
	br.Msg = "保存成功"
	br.Ret = 200
	return
}

// @Title 获取景区列表
// @Description 获取景区列表
// @Param	request	body models.ScenicSpotListReq true "type json string"
// @Success 200 {object} models.ScenicSpotPageResp
// @router /list [post]
func (this *ScenicSpotController) List() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	body := this.Ctx.Input.RequestBody
	req := new(models.ScenicSpotListReq)
	if string(body) != "" {
		err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
		if err != nil {
			br.Msg = "参数解析异常！"
			br.ErrMsg = "参数解析失败，Err:" + err.Error()
			return
		}
	}

	if req.PageSize <= 0 {
		req.PageSize = utils.PageSize20
	}
	if req.CurrentIndex <= 0 {
		req.CurrentIndex = 1
	}

	startSize := utils.StartIndex(req.CurrentIndex, req.PageSize)

	ssObj := new(models.ScenicSpot)

	var condition string
	var pars []interface{}

	if req.ScenicSpotName != "" {
		scenicSpotName := `%` + req.ScenicSpotName + `%`
		condition += "AND scenic_spot_name LIKE ?"
		pars = append(pars, scenicSpotName)

	}

	total, err := ssObj.GetScenicSpotPageCount(condition, pars)
	if err != nil {
		br.Msg = "获取数据失败！"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}

	list, err := ssObj.GetScenicSpotPage(condition, pars, startSize, req.PageSize)
	if err != nil {
		br.Msg = "获取数据失败！"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}

	page := utils.GetPaging(req.CurrentIndex, req.PageSize, total)
	resp := new(models.ScenicSpotPageResp)
	resp.List = list
	if err != nil {
		br.Msg = "获取数据失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	resp.Paging = page

	br.Msg = "获取成功"
	br.Ret = 200
	br.Data = resp
	return
}

// @Title 获取所有景区
// @Description 获取所有景区
// @Param	request	body models.ScenicSpotAllReq true "type json string"
// @Success 200 {object} models.ScenicSpotView
// @router /all [post]
func (this *ScenicSpotController) All() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	body := this.Ctx.Input.RequestBody
	req := new(models.ScenicSpotAllReq)
	if string(body) != "" {
		err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
		if err != nil {
			br.Msg = "参数解析异常！"
			br.ErrMsg = "参数解析失败，Err:" + err.Error()
			return
		}
	}

	ssObj := new(models.ScenicSpot)

	var condition string
	var pars []interface{}

	if req.ScenicSpotName != "" {
		roleName := "'%" + req.ScenicSpotName + "%'"
		condition += " scenic_spot_name like ?"
		pars = append(pars, roleName)
	}
	list, err := ssObj.GetScenicSpotAll(condition, pars)
	if err != nil {
		br.Msg = "获取数据失败！"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}

	br.Msg = "获取成功"
	br.Ret = 200
	br.Data = list
	return
}
